processNotification
processNotification
-
Start
- Receive
appointmentIdandisInternalfromNotificationDto.
- Receive
-
Fetch Appointment
-
Determine the repository to use:
- If
isInternal→ useappointmentRepo. - Else → use
appointmentEhrRepo.
- If
-
Fetch the completed appointment using:
appointmentIdbookingIdFieldbased on internal/external flag.
-
Log fetched appointment.
-
-
Check if Appointment Exists
-
If no appointment found:
- Log error.
- Return
{ success: true }.
-
-
Check for Existing Rating
-
Look for rating in
ratingAppointmentRepousingbookingId. -
If found:
- Return
{ success: true, message: 'This appointment has already been rated' }.
- Return
-
-
Prepare Customer Contact Details
-
Normalize the first phone number (US format).
-
Retrieve customer's email.
-
Fetch customer's opt-out preferences:
optedOutEmailoptedOutSMS
-
-
Determine Notification Types to Send
-
Initialize
typesToCheck:- If
!optedOutEmail→ addEMAIL. - If
!optedOutSMS→ addSMS.
- If
-
If
typesToCheckis not empty:- Call
fetchExistingRatingNotifications()to get existing sent types.
- Call
-
-
Build Notification Tasks
-
Initialize
ratingNotificationsToTrack = []. -
If Email should be sent:
-
Check:
!optedOutEmailcustomer.emailexistsEMAILnot inexistingNotifications
-
Then:
- Generate rating link.
- Call
sendEmailNotification(). - Add new
RatingNotificationtoratingNotificationsToTrack.
-
-
If SMS should be sent:
-
Check:
!optedOutSMScustomerPhoneNumberexistsSMSnot inexistingNotifications
-
Then:
- Generate rating link.
- Call
sendSmsNotification(). - Add new
RatingNotificationtoratingNotificationsToTrack.
-
-
-
Save Notification Records (If Any)
-
If
ratingNotificationsToTrack.length > 0:- Increment Prometheus counter
ratingsTypeProcessedCounterwith label'rated'. - Save records using
ratingNotificationRepo.save().
- Increment Prometheus counter
-
-
End
- Return
{ success: true }.
- Return